package com.jvtd.jpush;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.jpush.android.api.JPushInterface;

public class JvtdPushUtils
{
  public static boolean isEmpty(String s)
  {
    if (null == s)
      return true;
    if (s.length() == 0)
      return true;
    if (s.trim().length() == 0)
      return true;
    return false;
  }

  // 校验Tag Alias 只能是数字,英文字母和中文
  public static boolean isValidTagAndAlias(String s)
  {
    Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_!@#$&*+=.|]+$");
    Matcher m = p.matcher(s);
    return m.matches();
  }

  private static boolean isReadableASCII(CharSequence string)
  {
    if (TextUtils.isEmpty(string)) return false;
    try
    {
      Pattern p = Pattern.compile("[\\x20-\\x7E]+");
      return p.matcher(string).matches();
    } catch (Throwable e)
    {
      return true;
    }
  }

  public static String getDeviceId(Context context)
  {
    return JPushInterface.getUdid(context);
  }

  public static boolean isConnected(Context context)
  {
    ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = null;
    if (conn != null)
    {
      info = conn.getActiveNetworkInfo();
    }
    return (info != null && info.isConnected());
  }
}
